<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局  头部-->
        <tree-tools :tree-data="componany" @addDept="addDept" />
        <!-- 下面就是树形控件内容了 组织架构 -->
        <el-tree :data="departs" :props="defaultProps">
          <tree-tools
            slot-scope="{ data }"
            :tree-data="data"
            :is-root="false"
            @update="initData"
            @addDept="addDept"
            @editDept="editDept"
          />
        </el-tree>
      </el-card>
      <AddDept ref="addDept" :node-data="nodeData" @update="initData" />
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import { getDepartmentListData } from '@/api/department'
import treeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      componany: {
        name: '',
        manager: '负责人',
        id: ''
      },
      // 点击行的数据
      nodeData: null
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      // 发送请求
      const { depts, companyName } = await getDepartmentListData()
      // 赋值变量
      this.departs = tranListToTreeData(depts, '')
      this.componany.name = companyName
    },
    // 添加子部门
    addDept(val) {
      // 弹窗打开
      // this.isShow = true
      console.log(this.$refs.addDept)
      this.$refs.addDept.isShow = true
      // 记住是给谁添加的子部门
      this.nodeData = val
    },
    // 修改部门
    async editDept(val) {
      await this.$refs.addDept.getDepartmentInfo(val.id)
      this.$refs.addDept.isShow = true
      this.nodeData = val
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
